package com.woniuxy.common;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.woniuxy.entity.CarType;
import com.woniuxy.service.CarTypeService;

import java.util.ArrayList;
import java.util.List;

public class CarTypeExcelListener extends AnalysisEventListener<CarType> {

    //通过构造方法传入业务对象
    private CarTypeService carTypeService;

    public CarTypeExcelListener(CarTypeService carTypeService) {
        this.carTypeService = carTypeService;
    }

    //创建一个集合保存所有导入的数据
    private List<CarType> carTypeList = new ArrayList<>();

    //每条数据如何处理
    @Override
    public void invoke(CarType carType, AnalysisContext analysisContext) {
        //每条数据添加到集合中
        carTypeList.add(carType);
    }

    //数据导入完成后的操作
    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

        //写入到数据库中
         rowCount = carTypeService.insertBatch(carTypeList);
    }
    //影响行数
    private int rowCount;
    //获取导入的行数
    public int getRowCount() {
        return this.rowCount;
    }
}
